(function($) {
	$.pinyinSelect = function(selectObj, url) {
		$(document).ready(function() {
			selectObj.select2({
				ajax : {
					url : url,
					dataType : 'json',
					delay : 250,
					data : function(params) {
						return {
							inputVal : params.term,
						};
					},
					processResults : function(data) {
						data.unshift({
							id : "",
							text : "&nbsp;"
						});
						return {
							results : data
						};
					},
					cache : true
				},
				escapeMarkup : function(markup) {
					return markup;
				},
				minimumInputLength : 0
			});
		});
	};
})(jQuery);

function colSel(allCol, gridName, initcols) {
	
	if (allCol == undefined || allCol.length == 0)
		return allCol;
	var selColStr = localStorage.getItem("" + gridName);
	if (selColStr == undefined)selColStr= initcols;
	if (selColStr == undefined)return allCol;
	var showCols = new Array;
	var showColsArray = new Array;
	showCols = selColStr.split(",");
	if (showCols.length == 0)
		return allCol;
	var new_arr = [];
	for (var i = 0; i < showCols.length; i++) {
		var items = showCols[i];
		if ($.inArray(items, new_arr) == -1) {
			new_arr.push(items);
		}
	}
	// showCols.reverse();
	for (var i = 0; i < new_arr.length; i++) {
		if (isNaN(showCols[i].trim()) || new_arr[i].trim() == '')
			continue;
		if ((parseInt(new_arr[i].trim())) > allCol.length - 1)
			continue;
		if (allCol[new_arr[i].trim()] != undefined)
			showColsArray[i] = allCol[new_arr[i].trim()];
	}
	if (showColsArray.length == 0)
		return allCol;

	// $.parseJSON(d),
	showColsArray = jQuery.grep(showColsArray, function(a) {
		return a != undefined;
	});
	return showColsArray;
};
 
//不显示的变为HIDE
function colSel2(allCol, gridName, initcols) {
	
	if (allCol == undefined || allCol.length == 0)
		return allCol;
	var selColStr = localStorage.getItem("" + gridName);
	if (selColStr == undefined)selColStr= initcols;
	if (selColStr == undefined)return allCol;
	var showCols = new Array;
	var showColsArray = new Array;
	showCols = selColStr.split(",");
	if (showCols.length == 0)
		return allCol;
	var new_arr = [];
	for (var i = 0; i < showCols.length; i++) {
		var items = showCols[i];
		if ($.inArray(items, new_arr) == -1) {
			new_arr.push(items);
		}
	}
	for (var i = 0; i < allCol.length; i++) {
		  allCol[i].hidden=true;		 
	}
	
	// showCols.reverse();
	for (var i = 0; i < new_arr.length; i++) {
		if (isNaN(showCols[i].trim()) || new_arr[i].trim() == '')
			continue;
		if ((parseInt(new_arr[i].trim())) > allCol.length - 1)
			continue;
		if (allCol[new_arr[i].trim()] != undefined)
			allCol[new_arr[i].trim()].hidden=false;
	}
	if (showColsArray.length == 0)
		return allCol;
	
	// $.parseJSON(d),
	showColsArray = jQuery.grep(showColsArray, function(a) {
		return a != undefined;
	});
	return allCol;
};



function coLayer(allCol, gridTabName, initcols,dataurl,hiddeCols) {
	js.layer
			.open({
				type : 2,
				resize : false,
				title : [ '筛选显示', 'background-color:#e2e1e1;' ],
				area : [ '860px', '500px' ], // 宽高
				content :dataurl,
				shade : 0.5,
				closeBtn : 1,
				shadeClose : true,
				btn : [ '确认', '重置' ],
				yes : function(index, layero) {
					var selectedcols=layero.find('iframe')[0].contentWindow
					.$("#bootstrap-duallistbox-selected-list_doublebox option");
					if(selectedcols.length==0){
						parent.js.layer.alert('已选列不能为空！');return false;
					}
					var valstr = '';
					selectedcols.each(
									function(index, item) {
										valstr += $(this).val();
										if ((selectedcols.length - 1) != index)
											valstr += ",";

									});
					if (valstr.trim() != '')
						localStorage.setItem(gridTabName, valstr);

					parent.layer.closeAll();
					gridReload();
				},
				btn2 : function(index, layero) {
					localStorage.setItem(gridTabName, initcols);
					layero.find('iframe')[0].contentWindow.location.reload();
					return false;
				},
				success : function(layero, index) {
					var noSelCols = createNoSelColsData(allCol, gridTabName,hiddeCols);
					var selCols = createSelColsData(allCol, gridTabName);
					layero.find('iframe')[0].contentWindow.initCol(noSelCols,
							selCols, gridTabName, initcols);
				}
			});
};

function createNoSelColsData(allCol, gridName,hiddeCols) {
	//debugger;
	var allColArray = new Array;
	var allColArray2 = new Array;
	allColArray = allColArray2.concat(allCol);
	var selColStr = localStorage.getItem("" + gridName);
	var showColsArray = new Array;
	if (selColStr == undefined)
		selColStr= allColArray.join(',');
	var showCols = new Array;
	showCols = selColStr.split(",");
	if (showCols.length == 0 || allCol == undefined || allCol.length == 0)
		return allColArray;

	var new_arr = [];
	for (var i = 0; i < showCols.length; i++) {
		var items = showCols[i];
		if ($.inArray(items, new_arr) == -1) {
			new_arr.push(items);
		}
	}
	// showCols.reverse();
	for (var i = 0; i < new_arr.length; i++) {
		if (isNaN(new_arr[i].trim()) || new_arr[i].trim() == '')
			continue;
		if ((parseInt(new_arr[i].trim())) > allCol.length - 1)
			continue;
		if (allCol[new_arr[i].trim()] != undefined) {
			allColArray[new_arr[i].trim()] = undefined;
			// allColArray.splice(showCols[i].trim(),1);
		}
	}
	
	//去掉HIDDEN列
	var hiddeColsArr = new Array;
	if(hiddeCols!=undefined&&hiddeCols.indexOf(',')!=-1)hiddeColsArr = hiddeCols.split(",");
	for (var i = 0; i < hiddeColsArr.length; i++) {	
		if (isNaN(hiddeColsArr[i].trim()) || hiddeColsArr[i].trim() == '')
			continue;
		if ((parseInt(hiddeColsArr[i].trim())) > allCol.length - 1)
			continue;
		allColArray[hiddeColsArr[i].trim()] = undefined;
	}
	 
	// if(showColsArray.length==0)return allCol;
	allColArray = jQuery.grep(allColArray, function(a) {
		return a != undefined;
	});
	//console.log(allColArray);
	
	// $.parseJSON(d),
	return allColArray;
};

function createSelColsData(allCol, gridName) {

	var selColStr = localStorage.getItem("" + gridName);
	var showColsArray = new Array;
	if (selColStr == undefined)
		return showColsArray;
	var showCols = new Array;
	showCols = selColStr.split(",");
	if (showCols.length == 0 || allCol == undefined || allCol.length == 0)
		return showColsArray;

	var new_arr = [];
	for (var i = 0; i < showCols.length; i++) {
		var items = showCols[i];
		if ($.inArray(items, new_arr) == -1) {
			new_arr.push(items);
		}
	}
	// showCols.reverse();
	for (var i = 0; i < new_arr.length; i++) {
		if (isNaN(new_arr[i].trim()) || new_arr[i].trim() == '')
			continue;
		if ((parseInt(new_arr[i].trim())) > allCol.length - 1)
			continue;
		if (allCol[new_arr[i].trim()] != undefined)
			showColsArray[i] = allCol[new_arr[i].trim()];
	}
	;
	// if(showColsArray.length==0)return allCol;

	// $.parseJSON(d),
	showColsArray = jQuery.grep(showColsArray, function(a) {
		return a != undefined;
	});
	return showColsArray;
}